<template>
  <el-dialog :title="!dataForm.id ? '新增用户' : '修改用户'" :close-on-click-modal="false" :visible.sync="visible">

    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      :inline="true"
      :status-icon="true"
      label-width="130px"
      @keyup.enter.native="dataFormSubmit()"
    >

      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名" />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="密码" />
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名" />
      </el-form-item>

      <el-form-item label="手机" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机" />
      </el-form-item>

      <el-form-item label="部门" prop="deptId">
        <el-select v-model="dataForm.deptId" style="width: 189px;" placeholder="请选择部门" clearable>
          <el-option v-for="dept in deptList" :key="dept.id" :label="dept.name" :value="dept.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="岗位" prop="postId">
        <el-select v-model="dataForm.postId" style="width: 189px;" placeholder="请选择岗位" clearable>
          <el-option v-for="post in postList" :key="post.id" :label="post.name" :value="post.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" type="textarea" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
          active-text="启用"
          inactive-text="停用"
        />
      </el-form-item>

      <el-form-item label="头像" prop="headUrl">
        <single-upload v-model="dataForm.headUrl" />
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

  </el-dialog>

</template>

<script>
import api from '@/api/system/user'
import deptApi from '@/api/system/dept'
import postApi from '@/api/system/post'
import SingleUpload from '@/components/Upload/singleUpload'
export default {
  components: {
    SingleUpload
  },
  data() {
    var phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    var validatePwd = (rule, value, callback) => {
      if (this.dataForm.id) {
        callback()
      } else if (!this.dataForm.id && value === '') {
        callback(new Error('新增必须指定密码!'))
      }
    }
    return {
      visible: false,
      dataForm: {
        id: 0,
        username: '',
        password: '',
        name: '',
        phone: '',
        headUrl: '',
        deptId: '',
        postId: '',
        description: '',
        status: 1
      },
      // 部门列表
      deptList: [],
      // 岗位列表
      postList: [],
      dataRule: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePwd, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, pattern: phoneReg, message: '请输入正确手机号', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        postId: [
          { required: true, message: '请选择岗位', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请填写描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPostList()
  },
  methods: {
    // 数据初始化-修改
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()

        this.getDeptList()

        if (this.dataForm.id) {
          api.getInfoById(id).then(res => {
            this.dataForm.username = res.data.username
            this.dataForm.password = res.data.password
            this.dataForm.name = res.data.name
            this.dataForm.phone = res.data.phone
            this.dataForm.headUrl = res.data.headUrl
            this.dataForm.deptId = res.data.deptId
            this.dataForm.postId = res.data.postId
            this.dataForm.description = res.data.description
            this.dataForm.status = res.data.status
          })
        }
      })
    },
    // 部门列表
    getDeptList() {
      deptApi.getDataListForUserEdit().then(res => {
        this.deptList = res.data
      })
    },
    // 岗位列表
    getPostList() {
      postApi.getDataList({
        'page': 1,
        'limit': 500
      }).then(res => {
        this.postList = res.data.records
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          api.editData({
            'id': this.dataForm.id || undefined,
            'username': this.dataForm.username,
            'password': this.dataForm.password,
            'name': this.dataForm.name,
            'phone': this.dataForm.phone,
            'headUrl': this.dataForm.headUrl,
            'deptId': this.dataForm.deptId,
            'postId': this.dataForm.postId,
            'description': this.dataForm.description,
            'status': this.dataForm.status
          }, !this.dataForm.id ? 'save' : 'update').then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
